<?php   // $Id: fact_multi_facture_suite.php 347 2012-05-07 09:11:55Z pcoustillas $ ?>
<?php   
/*Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx
Modification : Pierre Coustillas*/

require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/language/$lang.php");
require_once("include/utils.php");

$acompte=isset($_POST['acompte'])?$_POST['acompte']:"";

$date_deb=isset($_POST['date_deb'])?$_POST['date_deb']:"";
$debut=date_to_sql($date_deb);

$date_fin=isset($_POST['date_fin'])?$_POST['date_fin']:"";
$fin=date_to_sql($date_fin);

$date_fact=isset($_POST['date_fact'])?$_POST['date_fact']:"";
$date_fact=date_to_sql($date_fact);

$coment=isset($_POST['coment'])?$_POST['coment']:"";

if($date_deb==''|| $date_fin=='' || $date_fact=='' )
{
	$message="$lang_oubli_champ";
	header('Location: fact_multi_facture.php?message=$message');
	exit;
}

$msg=""; // permettra d'envoyé un message .

//pour chaque client sélectionné
foreach($_POST['client'] as $client)
{
	
	$sql="SELECT count(*) FROM ${tblpref}bon_comm WHERE client_num = '$client' 
					AND ${tblpref}bon_comm.date >= '$debut' 
					and ${tblpref}bon_comm.date <= '$fin' 
					and num_facture = 0";
	$result = mysql_query($sql) or die("Erreur count nb_bon !<br/>$sql<br/>" . mysql_error());

	// il faut qu'il y ai au moins 1 bon pour créer la facture
	//echo mysql_result($result,1);
	if (mysql_result($result,0) >= 1)
	{

		$sql = " SELECT nom From ${tblpref}client WHERE num_client=$client ";
		$result = mysql_query($sql) or die("Erreur selection client n°$client !<br/>$sql<br/>" . mysql_error());
		$nom_client = mysql_result($result,0);

		//selection des bons de commandes de ce client qui n'ont pas été facturé
		$sql = "SELECT SUM(tot_htva) as total_htva, SUM(tot_tva) as total_tva  FROM ${tblpref}bon_comm WHERE client_num = '$client' 
					AND ${tblpref}bon_comm.date >= '$debut' 
					and ${tblpref}bon_comm.date <= '$fin' 
					and num_facture = 0";
	
		$result = mysql_query($sql) or die("Erreur selection bon non facturé !<br/>$sql<br/>" . mysql_error());


		$data=mysql_fetch_array($result);

		$total_htva = $data['total_htva'];
		$total_tva = $data['total_tva'];
		$total_ttc = $total_htva + $total_tva ;

		//on recherche le numero de la facture crée
		$sql = "SELECT MAX(num) As Maxi FROM ${tblpref}facture";
		$result = mysql_query($sql) or die('Erreur selection num facture max');
		$num_fact = mysql_result($result, 0);
		$num_fact = $num_fact + 1 ;

		// on met à jour les bon de commandes qui sont facturées
		$sql = "UPDATE ${tblpref}bon_comm SET num_facture=$num_fact
				WHERE ${tblpref}bon_comm.client_num = '$client' 
				AND ${tblpref}bon_comm.date >= '$debut'
				AND ${tblpref}bon_comm.date <= '$fin'
				AND num_facture = 0";

		mysql_query($sql) OR die("Erreur mise à jour des bons de commandes<br/>$sql<br/>" . mysql_error());

		//on crée la facture

		$sql1 = "INSERT INTO ${tblpref}facture(num,acompte, coment, client, date_fact, total_fact_h, total_fact_ttc)
			VALUES ('$num_fact','$acompte', '$coment', '$client', '$date_fact', '$total_htva', '$total_ttc')";
		//echo $sql1;
		mysql_query($sql1) or die("Erreur SQL1 !<br/>$sql1<br/>" . mysql_error());

		$msg.="Facture crée pour $nom_client<br/>";
	}

}

if ($msg=="")
{
	$msg=get_lang("Vous n'avez indiquer aucun clients");
}

header("location: fact_lister.php?message=$msg");

?>